﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using WebApp.Models;
using WebApp.Services;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WebApp.Controllers
{
    /// <summary>
    /// Power IoT 平台
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class IoTController : ControllerBase
    {
        //readonly IOptions<AppSettings> _settings;
        private readonly ILogger<IoTController> _logger;
        private readonly IPowerDataService _appService;
        private readonly IPowerHostService _powerservice;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="appService"></param>
        /// <param name="logger"></param>
        /// <param name="powerservice"></param>
        public IoTController(IPowerDataService appService, ILogger<IoTController> logger, IPowerHostService powerservice)
        {
            _logger = logger;
            _powerservice = powerservice;
            _appService = appService;
        }
        private bool Verify([Required] int appId, [Required] string signature, [Required] string timestamp, [Required] string nonce)
        {
            var app = _appService.GetApplication(appId);
            if (app == null)
                return false;
            var param = new string[] { app.AppToken, timestamp, nonce }.OrderBy(p => p);
            var input = string.Join("", param);
            //Console.WriteLine($"GetSha1Hash={Helper.GetSha1Hash(input)}");
            return Utility.VerifySha1Hash(input, signature);
        }
        /// <summary>
        /// 设备命令调用
        /// </summary>
        /// <param name="req">请求body</param>
        /// <param name="appId">应用Id</param>
        /// <param name="signature">签名</param>
        /// <param name="timestamp">时间戳</param>
        /// <param name="nonce">随机数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IoTCmdResult> DeviceCommandAsync([Required] IoTCmdReq req, [Required] int appId, [Required] string signature, [Required] string timestamp, [Required] string nonce)
        {
            if (!Verify(appId, signature, timestamp, nonce))
                return new IoTCmdResult { Status = IoTCmdStatus.None, Message = "请求无效" };

            return await _powerservice.DeviceCommandAsync(req);

        }

        /// <summary>
        /// 查询命令执行结果
        /// </summary>
        /// <param name="appId">应用Id</param>
        /// <param name="queryId">查询Id</param>
        /// <param name="signature">签名</param>
        /// <param name="timestamp">时间戳</param>
        /// <param name="nonce">随机数</param>
        /// <returns></returns>
        [HttpGet]
        public IoTCmdResult QueryDeviceCommand([Required] int appId, [Required] string queryId, [Required] string signature, [Required] string timestamp, [Required] string nonce)
        {
            if (!Verify(appId, signature, timestamp, nonce))
                return new IoTCmdResult { Status = IoTCmdStatus.None, Message = "请求无效" };

            return _powerservice.QueryDeviceCommand(queryId);

        }

    }
}
